package com.qf.scms.controller;

import com.qf.scms.param.LoginParam;
import com.qf.scms.param.RegisterParam;
import com.qf.scms.service.LoginService;
import com.qf.scms.utils.JwtUtils;
import com.qf.scms.utils.R;
import com.qf.scms.utils.UserUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author GaoQiangLiang
 * @date 2025-06-24 20:44
 */

@RestController
public class LoginController {

    @Resource
    private LoginService loginService;

    @Resource
    private JwtUtils jwtUtils;


    /**
     * 登录
     *
     * @param loginParam 登录参数
     * @return token令牌
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginParam loginParam) {
        String token = loginService.login(loginParam);
        return R.ok(token);
    }

    /**
     * 登出
     * @return 登出成功
     */
    @GetMapping("/logout")
    public R logout() {
        // 情况用户信息，防止内存泄漏
        UserUtils.remove();
        return R.ok();
    }

    /**
     *  注册
     * @param registerParam 注册参数
     * @return 注册结果，false表示注册失败，true表示注册成功
     */
    @PostMapping("/register")
    public R register(@RequestBody RegisterParam registerParam) {
        Boolean result = loginService.register(registerParam);
        if (result) {
            return R.ok("注册成功");
        }
        return R.fail("注册失败");
    }
}
